package com.yangming.service.impl;

import com.yangming.mapper.EmptyCartServiceMapper;
import com.yangming.mapper.RecordsMapper;
import com.yangming.mapper.SupplyMapper;
import com.yangming.model.EmptyCartService;
import com.yangming.model.Records;
import com.yangming.model.Supply;
import com.yangming.service.IRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 交易记录接口实现类
 * @author PengShuiMing
 * @create 2018-09-07
 **/
@Service
public class RecordsServiceImpl implements IRecordsService {
    @Autowired
    private SupplyMapper supplyMapper;
    @Autowired
    private EmptyCartServiceMapper emptyCartServiceMapper;
    @Autowired
    private RecordsMapper recordsMapper;


    @Override
    public int saveRecords(Integer serviceId, Integer supplyId, BigDecimal amount,String orderNo) {
        //获取空板车服务
        EmptyCartService emptyCartService = emptyCartServiceMapper.selectByPrimaryKey(serviceId);
        //获取货源
        Supply supply = supplyMapper.selectByPrimaryKey(supplyId);
        //新增记录
        Records records = new Records();
        records.setOrderNo(orderNo);//订单号
        records.setAmount(amount);//金额
        records.setServiceId(emptyCartService.getId());//服务id
        records.setServiceUserId(emptyCartService.getUserId());//服务用户id
        records.setSupplyId(supply.getId());//货源id
        records.setSupplyUserId(supply.getUserId());//货源用户id
        records.setDepartureId(supply.getDepartureId());//出发地id
        records.setDepartureName(supply.getDepartureName());//出发地名称
        records.setDepartureTime(supply.getDepartureTime());//出发时间
        records.setDestinationId(supply.getDestinationId());//目的地Id
        records.setDestinationName(supply.getDestinationName());//目的地名称
        records.setCreateTime(new Date());//创建时间
        return recordsMapper.insertSelective(records);
    }

    @Override
    public Boolean isServiceExist(Integer serviceId, Integer supplyUserId) {
        return recordsMapper.isServiceExist(serviceId,supplyUserId);
    }

    @Override
    public Records selectByOrderNo(String orderNo) {
        return recordsMapper.selectByOrderNo(orderNo);
    }

    @Override
    public int updateById(Records records) {
        return recordsMapper.updateByPrimaryKeySelective(records);
    }
}
